<?php
if (!defined('ROOT_PATH')) die('No direct access.');
function addArtikel($judul,$isi,$tag,$idpub,$idrev,$tipe){
	$db = new db();
	$tipe = ($tipe=='barang') ? 2 : 1;	//2=barang, 1=toko
	$slug = slugging($judul);
	$tgl = date('Y-m-d H:i:s', time());
	$query = "INSERT INTO Artikel (`tanggal`,`judul`,`slug-judul`,`isi`,`tag`,`type`,`idPublisher`,`idReview`) VALUES ('$tgl','$judul','$slug','$isi','$tag',$tipe,$idpub,$idrev)";
	$ret = 0;
	echo $query;
	//die();
	$db->exec($query);
	$ret = $db->lastInsertId();
	//echo $query;
	if ($ret==0){
		$_SESSION['message'] = "Gagal tambah artikel";
	}else{
		//update FB idPublisher
	}
	return $ret;
}

function editArtikel($judul,$isi,$tag,$index){
	$db = new db();
	$slug = slugging($judul);
	$query = "UPDATE Artikel SET `judul`='$judul',`slug-judul`='$slug',`isi`='$isi',`tag`='$tag' WHERE idArtikel=$index";
	$ret = 0;
	$db->exec($query);
	$ret = $db->rowCount();
	//echo $query;
	if ($ret==0) $_SESSION['message'] = "Gagal edit artikel";
	return $ret;
}

function getBlog($crt){
	//$db = new db();
	$query = "SELECT a.tanggal,a.judul,a.isi,a.tag,p.nama,a.idArtikel,a.`slug-judul`,a.type,a.idReview,p.idFB,b.nama as nama_barang,b.idBarang 
				FROM Artikel a
				INNER JOIN Pengguna p ON a.idPublisher = p.idPengguna
				INNER JOIN Barang b ON a.idReview = b.idBarang
				WHERE ";
	switch ($crt){
		case "fashion" :
			$query .= "kategori = 1 ";
			break;
		case "elektronik" :
			$query .= "kategori > 1 AND kategori < 5 ";
			break;
		case "kesehatan" :
			$query .= "kategori = 5 ";
			break;
		case "perlengkapan" :
			$query .= "kategori > 5 AND kategori < 9 ";
			break;
		case "seni" :
			$query .= "kategori > 8 AND kategori < 13 ";
			break;
		case "hobi" :
			$query .= "kategori = 13";
			break;
		case "kebun" :
			$query .= "kategori = 14";
			break;
		case "makanan" :
			$query .= "kategori = 15";
			break;
		case "lain" :
			$query .= "kategori=0";
			break;
		default:
			$query .= "1";
			break;
	}
	$query .= " ORDER BY tanggal DESC";
	//echo $query;
	//$ret = $db->query($query);
	return $query;
}

function getNewArt(){
	$db = new db();
	$query = "SELECT a.tanggal,a.judul,a.isi,a.tag,a.idArtikel,a.`slug-judul`,a.type,a.idReview
				FROM `Artikel` a
				ORDER BY tanggal DESC
				LIMIT 0,3";
	$ret = $db->query($query);
	return $ret;
}
